﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Skimpt3;
using Skimpt3.Plugins.Interface;

namespace ServerUpload
{
    class Plugin : IUploaderInterface
    {
        #region static variables
        //This is the name of the plugin as it will appear in the list of available
        //upload destinations in the program, thus it should be static.
        //EG: "Google"
        public string Name
        {
            get { return "My upload source"; }
        }

        //This is the script that the program will interact with to effect the image 
        //upload to the server, it should be static
        //EG http://www.example.com/upload.php 
        //or 
        //   http://www.example.com/ if the script is the index page 
        public string FormURL
        {
            get { return "http://my.upload.source.com/upload.php"; }
        }

        //This is the name of the file selector box that would normally appear on a web page
        //It should be static
        //EG "file"
        //In PHP, this is accessed as $_FILES["FileBoxName"]
        public string FileBoxName
        {
            get { return "fileBoxName"; }
        }
        #endregion

        #region non-static properties 
        //This stores the url pointing to the uploaded file and is used by the 
        //application to retrieve the URL
        //
        //In order for the application to function correctly, this variable must
        //be set during the upload function (using the private string)
        //
        //If the value is still null at the end of the upload method, the program will display
        // "No URL to report"
        private string strFileUrl = null;
        public string FileUrl
        {
            get { return strFileUrl; }
        }

        //This stores the url pointing to the thumbnail of the uploaded file and is used by the 
        //application to retrieve the URL
        //
        //In order for the application to function correctly, this variable must
        //be set during the upload function (using the private string)
        //
        //If the value is still null at the end of the upload method, the program will display
        // "No URL to report"
        private string strThumbUrl = null;
        public string ThumbUrl
        {
            get { return strThumbUrl; }
        }

        //This is the image object version of th image that will be uploaded
        //It is set automatically before the application executes the plugin 
        private Image imgImage = null;
        public Image Image
        {
            get
            {
                return imgImage;
            }
            set
            {
                imgImage = value;
            }
        }

        //This is the desired file name supplied by the user
        //It is automatically set before the application executes the plugin
        private string strFileName;
        public string FileName
        {
            get
            {
                return strFileName;
            }
            set
            {
                strFileName = value;
            }
        }
        #endregion

        #region upload method
        public void Upload()
        {
            //The following is an example of the absolute basic code needed to effect and image 
            //upload to the desired server and return the correct URLs
            //
            //(Taken from the original KalleLoad plugin 
            // which can be found in the main Skimpt solution under KalleLoad)

            Skimpt3.PostFile post = new Skimpt3.PostFile(FormURL, FileName);

            post.AddFormElement("MAX_UPLOAD_SIZE", "104857600");
            post.AddFormElement("progress", "Upload");
            post.FileBoxName = FileBoxName;
            post.ContentMIMEType = "image/jpeg";

            try
            {
                post.Send(Image);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            /*
             * Hopefully, post.Response will be returned as MIME:text/xml
             * In which case we can pass it in to a new System.XmlDocument and pass out the bits we want
             * System.XmlDocument: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
             *
             * Else it's going to be an HTML scraping job
             * Might also be able to pass that in to a new System.XmlDocument
             * Other wise it's substrings all the way :(
             */

            try
            {
                XmlDocument xDoc = new XmlDocument();
                Console.WriteLine(post.Response);
                xDoc.LoadXml(new System.IO.StringReader(post.Response).ReadToEnd());

                XmlNodeList nodeList = xDoc.GetElementsByTagName("property");

                strFileUrl = nodeList[0].InnerXml;
                strThumbUrl = nodeList[1].InnerXml;

                foreach (XmlNode node in nodeList)
                {
                    Console.WriteLine(node.Name + " content = " + node.InnerText + " ++ " + node.InnerXml + " ++ " + node.Value);
                    Console.WriteLine(node.Name + " outer = " + node.OuterXml);
                    foreach (XmlAttribute attr in node.Attributes)
                    {
                        Console.WriteLine(node.Name + "@" + attr.Name + " = " + attr.InnerText);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            /*
             * There could be more outputs here, such as:
             * ->BB Code
             * ->HTML Code
             * ->Thumbnail
             *
             * These would have to have their own fields:
             *
             * private string strFieldName = "";
             * public string FieldName
             * {
             * 		get { return strFieldName; }
             * }
             */
        }

        #endregion
    }
}
